package com.financing.wap.web.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.BizEnums.CommonEnums;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.common.SessionUtil;
import com.financing.wap.dto.FpShopProductTypeDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IFpShopProductTypeService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("fpShopProductTypeController")
@RequestMapping("/api/fpShopProductType")
public class FpShopProductTypeController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpShopProductTypeService fpShopProductTypeService;
	/***
	 * 用户自有产品分类
	 * @param request
	 * @param fpShopProductType
	 * @param pager
	 * @return
	 */
	@RequestMapping("/listFpShopProductType")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpShopProductTypeDTO fpShopProductType,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpShopProductType list begin .......");
			UserDTO user = SessionUtil.getSession().getUser();
			//查询用户下的分类列表和产品个数
			fpShopProductType.setRelUserId(user.getId());
			fpShopProductType.setDeleteTag(CommonEnums.DELETE_FLAG_1);
			List<FpShopProductTypeDTO> fpShopProductTypeList  = fpShopProductTypeService.queryList(fpShopProductType);
			
			result.setAttribute("list", fpShopProductTypeList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpShopProductType list end .......");
		}catch(Exception e){
			log.error("query fpShopProductType error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	/***
	 * 用户自有产品分类  新增
	 * @param request
	 * @param fpShopProductType
	 * @return
	 */
	@RequestMapping("/addFpShopProductType")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpShopProductTypeDTO fpShopProductType){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpShopProductType  begin .......");
			
			UserDTO user = SessionUtil.getSession().getUser();
			//查询用户下的分类列表和产品个数
			fpShopProductType.setRelUserId(user.getId());
			fpShopProductType.setDeleteTag(CommonEnums.DELETE_FLAG_1);
			DTOdateSetUtil.setCreateInfo(fpShopProductType);
			DTOdateSetUtil.setUpdateInfo(fpShopProductType);
			
			FpShopProductTypeDTO f=new FpShopProductTypeDTO();
			f.setTypeValue(fpShopProductType.getTypeValue());
			f.setRelUserId(user.getId());
			f.setDeleteTag(CommonEnums.DELETE_FLAG_1);
			
			FpShopProductTypeDTO queryListGetFirst = fpShopProductTypeService.queryListGetFirst(f);
			Integer oldId = queryListGetFirst.getId();
			if(queryListGetFirst!=null&&oldId!=null){
				result.setAttribute("id", oldId);
			}else{
				int id = fpShopProductTypeService.insert(fpShopProductType);
				result.setAttribute("id", id);
			}
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			logger.info("add fpShopProductType list end .......");
		}catch(Exception e){
			log.error("add fpShopProductType error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
